import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xin_he/page/mine/xin_he_adress_management/model/xin_he_address_item_model_entity.dart';
import 'package:xin_he/routes/xin_he_routes.dart';
import 'package:xin_he/utils/xh_styles.dart';

import '../../../utils/xin_he_image.dart';
import 'xin_he_adress_management_logic.dart';

class XinHeAdressManagementPage extends StatelessWidget {
  XinHeAdressManagementPage({Key? key}) : super(key: key);

  final logic = Get.put(XinHeAdressManagementLogic());
  final state = Get.find<XinHeAdressManagementLogic>().state;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        leading: IconButton(
            onPressed: (){
              Get.back();
            },
            icon: Container(
              padding: EdgeInsets.only(left: 7),
              alignment: Alignment.centerLeft,
              child: const XinHeAssetImage(
                'assets/images/home/app_back_3.png',
                width: 9.5,
                height: 17,
                fit: BoxFit.cover,),
            )
        ),
        title: const Text('地址管理',style: TextStyle(color: Colors.black , fontSize: 18,fontWeight: FontWeight.w600),),
        backgroundColor: Colors.white,
        shadowColor: Colors.transparent,
      ) ,
      body:Obx(() => Column(children: [
        Expanded(child:  ListView.separated(
          itemCount: state.list.length,
          itemBuilder: (ctx,index){
            XinHeAddressItemModelEntity model = state.list[index];
            return _itemView(model,index);},
          separatorBuilder: (BuildContext context, int index) {
            return SizedBox(height: 10,);
          },)),
        SizedBox(height: 10,),
        Container(
          margin:   EdgeInsets.only(left: 15,right: 15,bottom: Get.mediaQuery.padding.bottom+25),
          height: 44,
          width: double.infinity,
          decoration: BoxDecoration(color:XinHeColor('#30E9E9'),borderRadius: const BorderRadius.all(Radius.circular(22))),
          child: OutlinedButton(

            onPressed: (){
              Get.toNamed(XinHeRoutes.addAddress);
            },
            child:  const Text (
              "添加新地址",
              style: TextStyle (fontSize: 16,color:  Colors.white, fontWeight: FontWeight.bold) ,
              strutStyle: StrutStyle(
                fontSize: 16,
                leading: 0,
                height: 1.1,
                // 1.1更居中
                forceStrutHeight: true, // 关键属性 强制改为文字高度
              ),
            ),
          ),)
      ],)),

    );
  }

  Widget _itemView(XinHeAddressItemModelEntity model,int index){
    return Container(
      height: 146,
      padding: const EdgeInsets.symmetric(horizontal: 15),
      color: Colors.white,
      child: Column(children: [

        Container(
          height: 93.5,
          child: Row(children: [
            SizedBox(
              width: Get.width-30-16-10,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                SizedBox(height: 15,),
                Row(children: [
                  Text(model.name??'',style: TextStyle(color: XinHeColor('#222222'),fontSize: 14),),
                  SizedBox(width: 5,),
                  Text(model.telephone??'',style: TextStyle(color: XinHeColor('#555555'),fontSize: 15),),

                ],),

                SizedBox(height: 5,),
                Text('${model.province}${model.city}${model.area}${model.detail}',softWrap: true,style: TextStyle(color: XinHeColor('#777777'),fontSize: 12),),


              ],),
            ),

            Spacer(),

            GestureDetector(
              onTap: (){
                Get.toNamed(XinHeRoutes.addAddress,arguments: model);

              },
                child: const XinHeAssetImage('assets/images/mine/edit.png',width: 15,height: 16,)),

          ],),
        ),

        Container(
          width: Get.width-30,
          height: 0.5,
          color: XinHeStyle.lineColor,
        ),

        Container(
          height: 52,
            alignment: Alignment.center,
            color: Colors.white,
            child: Row(children: [

              GestureDetector(
                onTap:(){

                  if(model.isDefault != 1){
                    logic.settingItemDefault(model.id??0);
                  }

                  // for(int i = 0;i<state.list.length;i++){
                  //   if(i != index){
                  //     XinHeAddressItemModelEntity model = state.list[i];
                  //     model.isSelected = false;
                  //   }else{
                  //     if(!model.isSelected){
                  //       model.isSelected = !model.isSelected;
                  //       state.list[index] = model;
                  //     }
                  //
                  //   }
                  // }
                },
                  child: Container(
                    alignment: Alignment.center,
                      padding: const EdgeInsets.symmetric(vertical: 8).copyWith(right: 8),
                      child: XinHeAssetImage( model.isDefault == 1?'assets/images/cart/cart_selected.png':'assets/images/cart/cart_unselected.png',width:15 ,height: 15,))),
              Text('默认地址',style: TextStyle(color: XinHeColor('#222222'),fontSize: 12 ),),
              Spacer(),
              GestureDetector(
                onTap: (){
                  // state.list.remove(model);

                  logic.deleteItem(model.id??0);
                },
                child: Container(
                  alignment: Alignment.center,
                    padding: const EdgeInsets.symmetric(vertical: 8).copyWith(left: 8),
                    child: const XinHeAssetImage( 'assets/images/home/search_delete.png',width:16 ,height: 16,)),
              ),


            ],))

      ],),
    );
  }
}
